<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>

<div class="app">
    <section class="layout">
        <section class="main-content col-lg-12" id="expExportDiv">
            <div class="content-wrap">
                <div class="wrapper" style="bottom: 50px;">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/csm/expStock/batchImport')">批量入库</a>
                        </li>
                    </ul>
                    <section class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive no-border">
                                <div class="datatable-top">
                                    <div class="pull-left">
                                        <form:form id="searchForm" modelAttribute="expStock"
                                                   onsubmit="return loadDivForm('mainCenterDiv','searchFormDict','${ctx}/csm/expStock/batchImport');"
                                                   method="post" class="form-inline">
                                            <div class="form-group">
                                                <label class=" control-label">入库类别：</label>
                                                <form:select path="expImportMaster.importClass" id="batchImportClass" htmlEscape="false" class="form-control" maxlength="1"
                                                             onchange="loadSupplier()" data-parsley-required="true">
                                                    <form:options items="${his_fns:getImpOrOutpList('1')}" itemLabel="inOutName" itemValue="type" htmlEscape="false" />
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">入库日期：</label>
                                                <input name="importDate" id="importDate" htmlEscape="false" class="form-control Wdate" data-parsley-required="true" readonly/>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">供货方：</label>
                                                <form:select path="supplier" id="supplier" htmlEscape="false" onchange="addBatchImportList()" class="form-control" data-parsley-required="true">
                                                </form:select>
                                            </div>
                                            <div class="form-group">
                                                <label class=" control-label">入库金额总计:</label>
                                                <input class="form-control" id="totalMoney" type="text" readonly>
                                            </div>
                                            <div class="form-group">
                                                <input id="btnSubmit" class="btn btn-primary" type="button" onclick="saveExport()" value="保存" />
                                            </div>
                                        </form:form>
                                    </div>
                                </div>
                                <table id="contentTable" class="table table-bordered table-striped mg-t datatable">
                                    <thead>
                                    <tr>
                                        <th hidden>jsonText</th>
                                        <th >编码</th>
                                        <th>品名</th>
                                        <th>型号</th>
                                        <th hidden>规格</th>
                                        <th hidden>单位</th>
                                        <th hidden>包装数量</th>
                                        <th>包装规格</th>
                                        <th>包装单位</th>
                                        <th>厂家</th>
                                        <th>入库价</th>
                                        <th>批号</th>
                                        <th>有效期</th>
                                        <th>灭菌方式</th>
                                        <th>灭菌批号</th>
                                        <th>灭菌日期</th>
                                        <th>入库数量</th>
                                        <th>出库单号</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                </table>
                            </div>
                        </div>
                    </section>
                </div>
            </div>
        </section>
    </section>
</div>
<script type="text/javascript">
    var importList = [];  //入库数据
    var totalMoney = 0.00;   //入库金额总计
    addExportDate();
    //设置入库日期默认当前时间
    function addExportDate(){
        var date = new Date();
        var nowMonth = "";
        if (date.getMonth() + 1 <= 9) {
            nowMonth = "0" + (date.getMonth() + 1) + "";
        } else {
            nowMonth = (date.getMonth() + 1) + "";
        }
        var hours = "";
        if (date.getHours() < 10) {
            hours = "0" + date.getHours();
        } else {
            hours = date.getHours();
        }
        var minutes = "";
        if (date.getMinutes() < 10) {
            minutes = "0" + date.getMinutes();
        } else {
            minutes = date.getMinutes();
        }
        var seconds = "";
        if (date.getSeconds() < 10) {
            seconds = "0" + date.getSeconds();
        } else {
            seconds = date.getSeconds();
        }
        var now = date.getFullYear() + "-" + nowMonth + "-" + date.getDate() + " " + hours + ":" + minutes + ":" + seconds;
        $('#importDate').val(now);
    }

    //出库金额总计
    $('#totalMoney').val(0.00);

    loadSupplier();
    function loadSupplier() {
        $("#supplier").html("");  //供货单位
        var importClassValue = $('#batchImportClass').val();  //入库类别 代码
        $.ajax({
            url: '${ctx}/csm/expSupplierCata/getSupplierOrStorageList?inoutType=' + '1' + '&type=' + importClassValue,
            type: 'get',
            contentType: 'application/json',
            async: true,
            success: function (data, textStatus, jqXHR) {
                if (data.length > 0) {
                    if (typeof(data[0].supplierJc) == 'undefined') {
                        $.each(data, function (index, item) {
                            $("#supplier").append("<option value=''></option>");
                            $("#supplier").append("<option value='" + item.storageCode + "'>" + item.storageName + "</option>");
                        });
                    } else {
                        $.each(data, function (index, item) {
                            $("#supplier").append("<option value=''></option>");
                            $("#supplier").append("<option value='" + item.id + "'>" + item.supplierJc + "</option>");
                        });
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }

    //根据供货方加载需要批量入库的数据
    function addBatchImportList() {
        var storageId = $('#supplier').val();
        $.ajax({
            url: "${ctx}/csm/expExport/getBatchImportListData?storageId=" + storageId,
            type: 'get',
            contentType: 'application/json',
            async: true,
            success: function (data, textStatus, jqXHR){    //textStatus: success
                var jsonuserinfo = $("#searchForm").serializeObject();
                var datastr = JSON.stringify(jsonuserinfo);
                var trHTML = "";
                if(textStatus == 'success' && data.length > 0){
                    $.each(data,function(index,item){
                        totalMoney += item.expExportDetail.purchasePrice * item.expExportDetail.quantity;  //计算入库金额
                        $('#totalMoney').val(totalMoney);
                        importList.push(item);
                        trHTML = "<tr>";
                        trHTML += "<td hidden>" + datastr + "</td>";
                        trHTML += "<td>" + item.expExportDetail.expCode + "</td>";
                        trHTML += "<td>" + item.expDict.expName + "</td>";
                        trHTML += "<td>" + item.expDict.expModel + "</td>";
                        trHTML += "<td hidden>" + item.expExportDetail.minSpec + "</td>";
                        trHTML += "<td hidden>" + item.expExportDetail.minUnits + "</td>";
                        trHTML += "<td hidden>" + item.expExportDetail.packageNum + "</td>";
                        trHTML += "<td>" + item.expExportDetail.packageSpec + "</td>";
                        trHTML += "<td>" + item.expExportDetail.packageUnits + "</td>";
                        trHTML += "<td>" + item.expSupplierCata.supplierJc + "</td>";
                        trHTML += "<td>" + item.expExportDetail.purchasePrice + "</td>";
                        trHTML += "<td>" + item.expExportDetail.batchNo + "</td>";
                        if(typeof(item.expExportDetail.expireDate) == 'undefined'){
                            trHTML += "<td>" + '' + "</td>";
                        }else{
                            trHTML += "<td>" + item.expExportDetail.expireDate + "</td>";
                        }
                        trHTML += "<td>" + item.expExportDetail.sterilizationMethod + "</td>";
                        trHTML += "<td>" + item.expExportDetail.sterilizationBatchNo + "</td>";
                        if(typeof(item.expExportDetail.expireDate) == 'undefined'){
                            trHTML += "<td>" + '' + "</td>";
                        }else{
                            trHTML += "<td>" + item.expExportDetail.sterilizationDate + "</td>";
                        }
                        trHTML += "<td>" + item.expExportDetail.quantity + "</td>";
                        trHTML += "<td>" + item.expExportMaster.documentNo + "</td>";
                        trHTML += "</tr>";
                        $("#contentTable").append(trHTML);
                        document.getElementById('supplier').disabled = true;   //禁用供货方输入框
                    });
                }else{
                    document.getElementById('supplier').disabled = false;  //启用供货方输入框
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown){
                toastr.error(textStatus);
            }
        });
    }

    //入库保存
    function saveExport() {
        var batchImportClass = $('#batchImportClass').val();  //批量入库入库类别
        var batchImportDate = $('#importDate').val();    //入库日期
        var supplier = $('#supplier').val();    //供货方
        if(importList.length <= 0 || importList == []){
            toastr.error('没有要保存的数据!');
            return;
        }
        if (supplier.replace(/(^s*)|(s*$)/g, "").length == 0 || supplier == null || typeof(supplier) == 'undefined') {
            toastr.error("供货方不能为空,请选择");
            return;
        }
        var saveDataList = [];  //批量入库要保存的数据
        if(importList.length > 0){
            $.each(importList,function(index,item){
                var expStock = {};
                var expImportMaster = {};
                var expImportDetail = {};
                expImportDetail.expCode = item.expExportDetail.expCode;
                expImportDetail.infoId = item.expExportDetail.infoId;
                expImportDetail.minSpec = item.expExportDetail.minSpec;
                expImportDetail.minUnits = item.expExportDetail.minUnits;
                expImportDetail.packageNum = item.expExportDetail.packageNum;
                expImportDetail.packageSpec = item.expExportDetail.packageSpec;
                expImportDetail.packageUnits = item.expExportDetail.packageUnits;
                expImportDetail.purchasePrice = item.expExportDetail.purchasePrice;
                expImportDetail.batchNo = item.expExportDetail.batchNo;
                if(typeof(item.expExportDetail.expireDate) == 'undefined'){
                    expImportDetail.expireDate = '';
                }else{
                    expImportDetail.expireDate = item.expExportDetail.expireDate;
                }
                expImportDetail.sterilizationMethod = item.expExportDetail.sterilizationMethod;
                expImportDetail.sterilizationBatchNo = item.expExportDetail.sterilizationBatchNo;
                if(typeof(item.expExportDetail.sterilizationDate) == 'undefined'){
                    expImportDetail.sterilizationDate = '';
                }else{
                    expImportDetail.sterilizationDate = item.expExportDetail.sterilizationDate;  //灭菌日期
                }
                expImportDetail.quantity = Number(item.expExportDetail.packageNum) * Number(item.expExportDetail.quantity);
                expStock.expImportDetail = expImportDetail;
                expImportMaster.storageId = item.expExportMaster.receiver;
                expImportMaster.importDate = batchImportDate;
                expImportMaster.storageFrom = item.expExportMaster.storageId;
                expImportMaster.accountReceivable = totalMoney;
                expImportMaster.importClass = batchImportClass;
                expStock.expImportMaster = expImportMaster;
                var expExportMaster = item.expExportMaster;
                expStock.expExportMaster = expExportMaster;
                expStock.expCode = item.expExportDetail.expCode;
                expStock.infoId = item.expExportDetail.infoId;
                expStock.packageNum = item.expExportDetail.packageNum;
                expStock.packageSpec = item.expExportDetail.packageSpec;
                expStock.packageUnits = item.expExportDetail.packageUnits;
                expStock.minSpec = item.expExportDetail.minSpec;
                expStock.minUnits = item.expExportDetail.minUnits;
                expStock.purchasePrice = item.expExportDetail.purchasePrice;
                expStock.batchNo = item.expExportDetail.batchNo;
                if(typeof(item.expExportDetail.expireDate) == 'undefined'){
                    expStock.expireDate = '';
                }else{
                    expStock.expireDate = item.expExportDetail.expireDate;
                }
                expStock.sterilizationMethod = item.expExportDetail.sterilizationMethod;
                expStock.sterilizationBatchNo = item.expExportDetail.sterilizationBatchNo;
                if(typeof(item.expExportDetail.sterilizationDate) == 'undefined'){
                    expStock.sterilizationDate = '';
                }else{
                    expStock.sterilizationDate = item.expExportDetail.sterilizationDate;  //灭菌日期
                }
                expStock.quantity = expImportDetail.quantity;
                saveDataList.push(expStock);
            });
        }
        var dataListJson = JSON.stringify(saveDataList);
        console.log(dataListJson);
        $.ajax({
            type: "POST",
            url: '${ctx}/csm/expStock/saveBatchImport',
            data: dataListJson,
            'contentType': 'application/json',
            dataType: "json",
            success: function (data) {
                if (data.code == "success") {
                    toastr.success(data.data);
                    document.getElementById('supplier').disabled = false;  //启用供货方输入框
                    return loadDiv('mainCenterDiv', '${ctx}/csm/expStock/batchImport');
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }
</script>